#include "tContainer_t.h"
#include <vector>
#include <list>
#include <algorithm>
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <string>

enum{
	INT,
	STRING
};

static const int STRING_SIZE=200;

// Global variables
int select=-1;
bool exitMenu = false;

using namespace std;

void showStartup(int* select){
	cout<<"\nPlease select an option: \n";
	cout<<"1 - Create a Container of type: Integer\n";
	cout<<"2 - Create a Container of type: String \n";
	cout<<"3 - Exit \n";
	int subSelect;
	cin>>subSelect;
	while (subSelect<1 || subSelect>3){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"1 - Create a Container of type: Integer\n";
		cout<<"2 - Create a Container of type: String \n";
		cout<<"3 - Exit \n";
		cin>>subSelect;
		cin.ignore();
	}
	*select = subSelect;
}

void showActions(int* select){
	cout<<"\nWhat do you want to do? \n";
	cout<<"1 - Is the Container empty?\n";
	cout<<"2 - How many elements are there?\n";
	cout<<"3 - Insert a new element\n";
	cout<<"4 - Get first element\n";
	cout<<"5 - Get last element\n";
	cout<<"6 - Find an element by value\n";
	cout<<"7 - Remove a specific element and get his pointer\n";
	cout<<"8 - Remove all elements\n";
	cout<<"9 - Remove and delete a specific element\n";
	cout<<"10 - Remove and delete all elements\n";
	cout<<"11 - Exit \n";
	int subSelect;
	cin>>subSelect;
	while (subSelect<1 || subSelect>11){
		cout<<"\nPlease select a VALID option: \n";
		cout<<"1 - Is the Container empty?\n";
		cout<<"2 - How many elements are there?\n";
		cout<<"3 - Insert a new element\n";
		cout<<"4 - Get first element\n";
		cout<<"5 - Get last element\n";
		cout<<"6 - Find an element by value\n";
		cout<<"7 - Remove a specific element and get his pointer\n";
		cout<<"8 - Remove all elements\n";
		cout<<"9 - Remove and delete a specific element\n";
		cout<<"10 - Remove and delete all elements\n";
		cout<<"11 - Exit \n";
		cin>>subSelect;
		cin.ignore();
	}
	*select = subSelect;
}

char* trueOrFalse(bool b){
	if (b) return "True";
	return "False";
}

void IntContainer(){
	tContainer_t<int, vector <int *>> t;
	int* pElement;
	while(!exitMenu){
			showActions(&select);
			switch(select)
			{
				case 1: {
					cout << trueOrFalse(t.empty()) << endl;
					break;
						}
				case 2: {
					cout << t.size() << endl;
					break;
						}
				case 3: {
					int *elem = new int[1];
					cout << "Write the element you want to insert:" << endl;
					cin >> *elem;
					t.push_back(*elem);
					break;
						 }
				case 4: {
					pElement  = t.back();
					if (pElement == NULL) {
						cout << "Error: getting last element" << endl;
					}
					cout << *(pElement) << endl << endl;
					break;
						}
				case 5: {
					pElement  = t.front();
					if (pElement == NULL) {
						cout << "Error: getting first element" << endl;
					}
					cout << *(pElement) << endl << endl;
					break;
						}
				case 6: {
					int elem;
					cout << "Write the element you want to search:" << endl;
					cin >> elem;
					pElement = t.find(elem);
					if (pElement==NULL){
						cout << "Element was not found." << endl << endl;
					}
					break;
						}
				case 7: {
					int elem;
					cout << "Write the element you want to remove:" << endl;
					cin >> elem;
					pElement = t.remove_specific(elem);
					if (pElement==NULL){
						cout << "Element was not found." << endl << endl;
					}

					break;
						}
				case 8: {
					t.remove_all();
					break;
						}
				case 9: {
					int elem;
					cout << "Write the element you want to remove and delete:" << endl;
					cin >> elem;
					bool bRes;
					bRes = t.remove_delete_specific(elem);
					if (bRes==false){
						cout << "Element was not found." << endl << endl;
					}
					break;
						}
				case 10: {
					t.remove_delete_all();
					break;
						}
				case 11: {
					exitMenu = true;
					break;
						}

				default: {
					exitMenu = true;
					break;
						}
			}
		
		cout << "Press any key to continue" << endl << endl;
		getch();
	}
}

void StringContainer(){
	tContainer_t<string, vector <string *>> t;
	string *pElement;
	while(!exitMenu){
			showActions(&select);
			switch(select)
			{
				case 1: {
					cout << trueOrFalse(t.empty()) << endl;
					break;
						}
				case 2: {
					cout << t.size() << endl;
					break;
						}
				case 3: {
					string *elem = new string();
					cout << "Write the element you want to insert:" << endl;
					cin >> *elem;
					t.push_back(*elem);
					break;
						 }
				case 4: {
					pElement  = t.back();
					if (pElement == NULL) {
						cout << "Error: getting last element" << endl;
					}
					cout << *(pElement) << endl << endl;
					break;
						}
				case 5: {
					pElement  = t.front();
					if (pElement == NULL) {
						cout << "Error: getting last element" << endl;
					}
					cout << *(pElement) << endl << endl;
					break;
						}
				case 6: {
					string *elem = new string();
					cout << "Write the element you want to search:" << endl;
					cin >> *elem;
					pElement = t.find(*elem);
					if (pElement==NULL){
						cout << "Element was not found." << endl << endl;
					}
					delete elem;
					break;
						}
				case 7: {
					string *elem = new string();
					cout << "Write the element you want to remove:" << endl;
					cin >> *elem;
					pElement = t.remove_specific(*elem);
					if (pElement==NULL){
						cout << "Element was not found." << endl << endl;
					}
					delete elem;

					break;
						}
				case 8: {
					t.remove_all();
					break;
						}
				case 9: {
					string *elem = new string();
					cout << "Write the element you want to remove and delete:" << endl;
					cin >> *elem;
					bool bRes;
					bRes = t.remove_delete_specific(*elem);
					if (bRes==false){
						cout << "Element was not found." << endl << endl;
					}
					delete elem;
					break;
						}
				case 10: {
					t.remove_delete_all();
					break;
						}
				case 11: {
					exitMenu = true;
					break;
						}

				default: {
					exitMenu = true;
					break;
						}
			}
		
		cout << "Press any key to continue" << endl << endl;
		getch();
	}
}


int main(){

	showStartup(&select);
			
	switch(select)
	{
		case 1: {
			IntContainer();
			break;
				}
		case 2: {
			StringContainer();
			break;
				}
		case 3: {
			break;
				}
		}
	return 0;
}